package cn.hhchat.wp.config.security;

import cn.hhchat.wp.security.service.JwtAuthenticationEntryPoint;
import cn.hhchat.wp.security.service.JwtAuthenticationTokenFilter;
import cn.hhchat.wp.security.service.JwtUserDetailsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * Created this one by HMH on 2017/6/28.
 * 拦截请求，检查Token存在，获取用户及权限放在UserDefine中
 */
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{

    @Value("${server.servlet-path}")
    public String servletPath;

    @Bean
    public JwtAuthenticationTokenFilter
    authenticationTokenFilterBean(){
        return new JwtAuthenticationTokenFilter();
    }


    protected void configure(HttpSecurity http) throws Exception {
        http
                // we don't need CSRF because our token is invulnerable
                .csrf().disable()

                .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()

                // don't create session
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()

                .authorizeRequests()
//                .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
                //不需要微信授权的页面，只有获取url的页面
                //微信的中心数据
                // allow anonymous resource requests
                .antMatchers(
                        HttpMethod.GET,
                        servletPath + "/static/**",
                        servletPath + "/initAll",
                        "/**/*.html",
                        "/../*.css",
                        "/../*.js",
                        servletPath + "/favicon.ico"
                ).permitAll()
                .antMatchers(
                        HttpMethod.POST,
                        servletPath + "/upload",
                        servletPath + "/login",
                        servletPath + "/favicon.ico",
                        "/**/*.html",
                        "/**/*.css",
                        "/**/*.js"
                ).permitAll()
                .antMatchers(HttpMethod.PUT,
                        servletPath + "/user")
                .permitAll()
                .antMatchers(HttpMethod.OPTIONS, "/**", servletPath + "/upload").permitAll()
                .anyRequest().authenticated();
        http.sessionManagement().sessionFixation().none();
        http.addFilterBefore(authenticationTokenFilterBean(), UsernamePasswordAuthenticationFilter.class);
        // disable page caching
        http.headers().cacheControl();
    }

    @Autowired
    public void configureAuthentication(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
        authenticationManagerBuilder
                .userDetailsService(userDetailsService);
    }

    @Autowired
    private JwtAuthenticationEntryPoint unauthorizedHandler;

    @Autowired
    private JwtUserDetailsServiceImpl userDetailsService;

}
